//
//  DetailViewController.m
//  22-ALiMusic
//
//  Created by dllo on 16/8/15.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "DetailViewController.h"
#import "RandomPlayTableViewCell.h"
#import "SongTableViewCell.h"
#import "AFNetworking.h"
#import "HeadView.h"
#import "SongDetail.h"
#import "SongData.h"
#import "SongList.h"
#import "UIImageView+WebCache.h"
#define kTableViewReuse @"reuse"
#define kTableViewSongReuse @"songReuse"
@interface DetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *songDetailTableView;
@property (nonatomic, strong) SongDetail *songDetail;
@property (nonatomic, strong) UIImageView *picImageView;
@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getDataFromNet];
    [self creatTableView];
    NSLog(@"URL = %@", [NSString stringWithFormat:@"http://api.dongting.com/song/album/%@", _valueString]);
    
}

- (void)getDataFromNet {
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/album/%@", _valueString];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        self.songDetail = [SongDetail modelWithDic:dic];
        [_songDetailTableView reloadData];
        
        [_picImageView sd_setImageWithURL:[NSURL URLWithString:_songDetail.data.picUrl]];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}

- (void)creatTableView {
    self.songDetailTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _songDetailTableView.delegate = self;
    _songDetailTableView.dataSource = self;
    [self.view addSubview:_songDetailTableView];
    [_songDetailTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewReuse];
    [_songDetailTableView registerNib:[UINib nibWithNibName:@"SongTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTableViewSongReuse];
    
    self.picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 200)];
    _songDetailTableView.tableHeaderView = _picImageView;

}

#pragma mark - TableView Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HeadView *headView = [[HeadView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 50)];
    return headView;
}
#pragma mark - TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _songDetail.data.songList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SongTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewSongReuse];
    cell.numberLabel.text = @(indexPath.row + 1).description;
    cell.songList = _songDetail.data.songList[indexPath.row];
    return cell;
}
#pragma mark - Memory Warning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
